package admindao

import (
	"go.uber.org/zap"
	"strings"
	"wzgames/internal/admin/api/wzabind"
	"wzgames/internal/admin/model"
	"wzgames/internal/gdao"
	"wzgames/pkg/logger"
)

type UserDao gdao.DaoSource

const sqlWzUserCount = "select count(*) from user a"

func (dao *UserDao) GetWzUserTotal(req wzabind.WzUserReq) (int, error) {
	var c int
	var sb strings.Builder
	sb.WriteString(sqlWzUserCount)
	s1, args := dao.genWhere(req)
	sb.WriteString(s1)
	err := dao.Get(&c, sb.String(), args...)
	return c, err
}

func (dao *UserDao) genWhere(req wzabind.WzUserReq) (string, []interface{}) {
	var sb strings.Builder
	var items []string
	var args []interface{}

	if req.Uid > 0 {
		items = append(items, " a.`uid`=? ")
		args = append(args, req.Uid)
	}
	if req.AgtId > 0 {
		items = append(items, " a.`agtid`=? ")
		args = append(args, req.AgtId)
	}
	if req.Uname != "" {
		items = append(items, " a.`uname` like CONCAT('%',?,'%') ")
		args = append(args, req.Uname)
	}
	if req.AgtUid != "" {
		items = append(items, " a.`agtuid` like CONCAT('%',?,'%') ")
		args = append(args, req.AgtUid)
	}

	if len(items) > 0 {
		sb.WriteString(strings.Join(items, " and "))
	}
	return sb.String(), args
}

const sqlGetWzUsers = "select a.uid,a.uname,a.agtid,a.agtuid,a.status,a.created,b.instid,b.currid,b.gameid,IFNULL(INET6_NTOA(b.uip), '') as uip,b.updated from user a join user_latest b on a.uid=b.uid"

func (dao *UserDao) GetWzUsers(req wzabind.WzUserReq, offset, pageSize int) ([]*model.UserInfoEntity, error) {
	var sb strings.Builder
	sb.WriteString(sqlGetWzUsers)
	s1, args := dao.genWhere(req)
	if len(args) > 0 {
		sb.WriteString(" where ")
		sb.WriteString(s1)
	}
	if pageSize == 0 {
		sb.WriteString(" order by b.updated desc")
	} else {
		sb.WriteString(" order by b.updated desc limit ?,?")
		args = append(args, offset, pageSize)
	}
	rows, err := dao.Queryx(sb.String(), args...)
	if err != nil {
		logger.Error("err == ", zap.String("error", err.Error()))
		return nil, err
	}
	var ret []*model.UserInfoEntity
	for rows.Next() {
		rd := &model.UserInfoEntity{}
		err = rows.StructScan(rd)
		if err != nil {
			logger.Error("err == ", zap.String("error", err.Error()))
			return nil, err
		}
		ret = append(ret, rd)
	}
	return ret, err
}

const sqlGetGameLoginLog = "select `uid`,`instid`,`nodeid`,`currid`,`gameid`,IFNULL(INET6_NTOA(`ip`), '') as ip,`dateline`,`uname`,`agtid`,`agtuid`,`device` from user_login_games where `uid`=?"

func (dao *UserDao) GetWzUserLoginGameLog(req wzabind.WzUserReq, offset int) ([]*model.UserGameLoginEntity, error) {
	var sb strings.Builder
	var args []interface{}
	sb.WriteString(sqlGetGameLoginLog)
	args = append(args, req.Uid)
	if req.PageSize == 0 {
		sb.WriteString(" order by `dateline` desc")
	} else {
		sb.WriteString(" order by `dateline` desc limit ?,?")
		args = append(args, offset, req.PageSize)
	}
	rows, err := dao.Queryx(sb.String(), args...)
	if err != nil {
		logger.Error("err == ", zap.String("error", err.Error()))
		return nil, err
	}
	var ret []*model.UserGameLoginEntity
	for rows.Next() {
		rd := &model.UserGameLoginEntity{}
		err = rows.StructScan(rd)
		if err != nil {
			logger.Error("err == ", zap.String("error", err.Error()))
			return nil, err
		}
		ret = append(ret, rd)
	}
	return ret, err
}
